<template>
  <view class="settle-box">
    <label class="radio">
      <radio color="#C00000" :checked="isAllChecked" @click="changeAllStatus" /><text>全选</text>
    </label>
    <view class="num-total">
      <text>合计：</text>
      <text>￥{{totalPrice}}</text>
    </view>
    <view class="btn-settle" @click="settlement">结算（{{checkCount}}）</view>
  </view>
</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my-settle ",
    data() {
      return {
        second: 3,
        timer: null
      };
    },
    computed: {
      ...mapGetters('m_cart', ['checkCount', 'total', 'totalPrice']),
      ...mapGetters('m_user', ['addstr']),
      ...mapState('m_user', ['token', 'redirectInfo']),
      ...mapState('m_cart', ['cart']),
      isAllChecked() {
        return this.total === this.checkCount
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateAllchecked']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      changeAllStatus() {
        this.updateAllchecked(!this.isAllChecked)
      },
      settlement() {
        // 1. 先判断是否勾选了要结算的商品
        if (!this.checkCount) return uni.$showMsg('请选择要结算的商品！')

        // 2. 再判断用户是否选择了收货地址
        if (!this.addstr) return uni.$showMsg('请选择收货地址！')

        // 3. 最后判断用户是否登录了
        if (!this.token) return this.delayNavigate()
        this.payOrder()
      },
      async payOrder() {
        const goods = this.cart.filter(item => item.status).map(item => ({
          goods_id: item.goods_id,
          goods_number: item.goods_count,
          goods_price: item.goods_price
        }))
        const orderInfo = {
          order_price: this.totalPrice,
          consignee_addr: this.addstr,
          goods: goods
        }
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
        // 1.3 得到服务器响应的“订单编号”
        const orderNumber = res.message.order_number
      },
      delayNavigate() {
        this.second = 3
        this.showTips(this.second)
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my'
            })
            this.updateRedirectInfo('/pages/cart/cart')
            return
          }
          this.showTips(this.second)
        }, 1000)
      },
      showTips(n) {
        uni.showToast({
          // 不展示任何图标
          icon: 'none',
          // 提示的消息
          title: '请登录后再结算！' + n + ' 秒后自动跳转到登录页',
          // 为页面添加透明遮罩，防止点击穿透
          mask: true,
          // 1.5 秒后自动消失
          duration: 1500
        })
      }
    }
  }
</script>

<style lang="scss">
  .settle-box {
    border-top: 1px solid #efefef;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    height: 50px;
    bottom: 0;
    left: 0;
    width: 100%;
    // background-color: antiquewhite;
    font-size: 14px;

    .radio {
      padding-left: 10px;
      display: flex;
      align-items: center;
    }

    .num-total {
      text {
        &:nth-child(2) {
          font-weight: bold;
          color: #c00000;
        }
      }
    }

    .btn-settle {
      width: 100px;
      color: white;
      text-align: center;
      line-height: 50px;
      background-color: #c00000;
    }
  }
</style>